package com.travel.server.manager.impl.command.question;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.question.DeleteQuestionRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.QuestionnaireDal;
import com.travel.server.manager.model.QuestionnaireInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\15 0015 22:24
 * @Desc:
 */
@Component
@Slf4j
public class DeleteQuestionCommand extends AbstractCommand<DeleteQuestionRequest, EmptyResponse> {

    @Autowired
    private QuestionnaireDal questionnaireDal;

    @Override
    protected void checkInput(DeleteQuestionRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteQuestionRequest request) {
        checkInput(request);
        UserInfo userInfo = getUserInfo();
        QuestionnaireInfo questionnaireInfo = questionnaireDal.selectById(request.getId());
        questionnaireInfo.setState(CommonConstant.NO);
        questionnaireInfo.setUpdateBy(userInfo.getName());
        questionnaireInfo.setUpdateTime(new Date());
        questionnaireDal.insertOrUpdate(questionnaireInfo);
        return new EmptyResponse();
    }
}
